

<?php
use Core\Config;

$domainItem = $this->domainItem;
$allUnits = $this->allUnits;

$lang = \Core\Session\FrontSessionHelper::getCurrentLang();
$defaultLang =  Config::getDefaultLanguage();

?>
<style type="text/css">

</style>
<hr />
<ol class="breadcrumb bc-3">
    <li>
        <a href="/<?php echo $domainItem->domain ?>/admin"><i class="entypo-home"></i>Home</a>
    </li>
    <li class="active">
        <strong>Đơn vị thực đơn</strong>
    </li>

</ol>
<div class="row">
    <div class="col-sm-6">
        <h3>Danh sách đơn vị thực đơn</h3>
    </div>
    <div class="col-sm-3" style="padding: 15px 0px 0px 15px">
        <a class="btn btn-blue btn-icon btn-sm" type="button" href="javascript:showAddWindow()">
            Thêm mới
            <i class="entypo-plus"></i>
        </a>
    </div>

</div>
<div class="row">
    <div class="col-sm-6">
        <table class="table table-responsive" id='tblUnit'>
            <thead>
            <tr>
                <th>#</th>
                <th>Tên</th>
                <th></th>
            </tr>
            </thead>
            <tbody>
            <?php
            $i = 1;
            if($allUnits)
                foreach ($allUnits as $aUnit) {
                    echo '<tr>
                                    <td>'.$i.'</td>
                                    <td>'.$aUnit['name'].'</td>
                                    <td>
                                    <a href="javascript: void(0);" onclick="doEdit(this, '.$aUnit['id'].', \''.$aUnit['name'].'\')" ><i class="entypo-pencil"></i>Sửa</a>&nbsp;&nbsp;&nbsp;
                                    <a href="javascript: void(0);" onclick="doDelete(this,'.$aUnit['id'].')"><i class="entypo-cancel"></i>Xóa</a>
                                    </td>
                                </tr>';
                    $i++;
                }
            ?>
            </tbody>
        </table>
    </div>

</div>

<div class="hide" id="unitAddWindow">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title"></h4>
            </div>
            <div class="modal-body" >

                <dl>
                    <?php
                        if(\Core\Session\FrontSessionHelper::getCurrentLang() != Config::getDefaultLanguage()){
                            echo '<dt style="padding: 5px">
                    <div class="row form-group">
                        <label class="col-sm-3 control-label" >Giá trị tiếng Việt:</label>
                        <div class="col-sm-6" id="divViValue">

                        </div>
                    </div>
                    </dt>';
                        }
                    ?>


                    <dt style="padding: 5px">
                    <div class="row form-group">
                        <label class="col-sm-3 control-label" for="txtName">Tên(*):</label>
                        <div class="col-sm-6">
                            <input  id='txtUnitId'  name="txtUnitId" type="hidden"    value=''>
                            <input  id='txtUnitName' class='form-control' name="txtUnitName" required  value='' placeholder="Nhập tên, bắt buộc">
                        </div>
                    </div>
                    </dt>
                    <dt style="padding: 5px">
                        <span style="color:red" id="spnStatus"></span>
                    </dt>
                </dl>
            </div>
            <div class="modal-footer">
                <a type="button" class="btn btn-default" data-dismiss="modal" >Đóng</a>
                <a type="button" class="btn btn-primary" id="btnSaveUnit">Lưu</a>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>

<script>

    var lang = '<?php echo $lang?>';
    var defaultLang = '<?php echo $defaultLang?>';
    $(document).ready(function(){

        windowHtml = $('#unitAddWindow').html();
        $('#myWindow').html(windowHtml);

        $('#myWindow').on('shown.bs.modal', function () {
            $('#myWindow #txtUnitName').focus();
        })

        $('#myWindow #btnSaveUnit').click(function(){
            name = $('#myWindow #txtUnitName').val();
            id = $('#myWindow #txtUnitId').val();
            $.post('<?php echo '/'.$domainItem->domain.'/admin/catalog/save-unit'  ?>',
                {name:name,id:id}
                ,function(result){
                    if (result.success){
                        $('#myWindow').modal('toggle');
                        $("#myWindow #spnStatus").html('');
                        $('#tblUnit tr').each(function(){
                            $(this).css('background-color', '#fff');
                        })
                        if(!id){
                            index =  parseInt($('#tblUnit tr:last td:first').html()) + 1;
                            $('#tblUnit tr:last').after('<tr>'+
                                '<td>'+index+'</td>'+
                                '<td>'+result.data.name+'</td>'+
                                '<td>'+
                                '<a href="javascript: void(0);" onclick="doEdit(this, '+result.data.id+', \''+result.data.name+'\')" ><i class="entypo-pencil"></i>Sửa</a>&nbsp;&nbsp;&nbsp;'+
                                '<a href="javascript: void(0);" onclick="doDelete(this, '+result.data.id+')"><i class="entypo-cancel"></i>Xóa</a>'+
                                '</td>'+
                                '</tr>');

                            $('#tblUnit tr:last').css('background-color', '#46d4fa');
                        }else{
                            index = $(currentRow).find('td:eq(0)').html();

                            $(currentRow).html('<td>'+index+'</td>'+
                                '<td>'+result.data.name+'</td>'+
                                '<td>'+
                                '<a href="javascript: void(0);" onclick="doEdit(this, '+result.data.id+', \''+result.data.name+'\')" ><i class="entypo-pencil"></i>Sửa</a>&nbsp;&nbsp;&nbsp;'+
                                '<a href="javascript: void(0);" onclick="doDelete(this, '+result.data.id+')"><i class="entypo-cancel"></i>Xóa</a>'+
                                '</td>');
                            $(currentRow).css('background-color', '#46d4fa');
                        }

                    }else{
                        $("#myWindow #spnStatus").html(result.msg);
                    }
                },'json');
        })

        $("#myWindow").keypress(function(event){
            //if the key press is ESC
            if (event.keyCode === 27) {
                $('#myWindow').modal('toggle');
            }

            if(event.keyCode === 13) {
                saveUnit();
            }
        });

        function saveUnit(){
            $('#myWindow #btnSaveUnit').trigger('click');
        }
    })


    var opts = {
        "closeButton": true,
        "debug": false,
        "positionClass": "toast-bottom-right",
        "onclick": null,
        "showDuration": "300",
        "hideDuration": "1000",
        "timeOut": "5000",
        "extendedTimeOut": "1000",
        "showEasing": "swing",
        "hideEasing": "linear",
        "showMethod": "fadeIn",
        "hideMethod": "fadeOut"
    };


    function setWindowTitle(ctnId, title){
        $('#'+ ctnId + ' .modal-dialog .modal-content .modal-header h4').text(title)
    }


    function showAddWindow(){
        if(lang != defaultLang){
            bootbox.confirm("Bạn cần thêm đơn vị Tiếng Việt trước.<br> Bạn có muốn chuyển chức năng này sang Tiếng Việt không?", function(result) {
                   if(result)
                       changeLang(defaultLang);
            });
            return;
        }

        setWindowTitle('myWindow', 'Thêm đơn vị mới ');
        $('#myWindow #txtUnitName').val('');
        $('#myWindow #txtUnitId').val('');
        $('#myWindow').modal();

    }

    function doEdit(me, id, name){

        tr = $(me).closest('tr');
        currentRow = tr;
        setWindowTitle('myWindow', 'Sửa đơn vị: '+name);
        $('#myWindow #txtUnitName').val(name);
        $('#myWindow #txtUnitId').val(id);
        $('#myWindow').modal();
        if(lang != defaultLang){
            $.post('<?php echo '/'.$domainItem->domain.'/admin/catalog/get-unit-default-name'  ?>',
                {id:id}
                ,function(result){
                    if (result.success){
                        $('#myWindow #divViValue').html(result.value);
                    }else{
                        toastr.error("Lỗi: "+result.msg, "", opts);
                    }
                },'json');
        }

    }

    function doDelete(me, id){
        tr = $(me).closest('tr');
        currentRow = null;

        bootbox.confirm("Bạn chắc chắn muốn xóa đơn vị này?", function(result) {
            if(result){
                $.post('<?php echo '/'.$domainItem->domain.'/admin/catalog/delete-unit'  ?>',
                    {id:id}
                    ,function(result){
                        if (result.success){

                            $('#tblUnit tr').each(function(){
                                $(this).css('background-color', '#fff');
                            })
                            tr.remove();
                            toastr.info("Xóa thành công!", "", opts);

                        }else{
                            toastr.error("Lỗi: "+result.msg, "", opts);
                        }
                    },'json');
            }

        });

    }
    var currentRow;
</script>
